import { _decorator, Component, Node, BoxCollider2D, CircleCollider2D, Contact2DType, IPhysics2DContact, Sprite, UITransform, Vec3, Size, Vec2 } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
const { ccclass, property } = _decorator;

@ccclass('Wall2D')
export class Wall2D extends BasicComponet {
    //叠加的速度
    addSpd = 10;
    maxX = 0;
    minX = 0;

    setData(d: { parent: Node, p: Vec3, r: Vec3, s: Vec2 }) {
        this.node.parent = d.parent;
        this.node.setPosition(d.p);
        this.node.eulerAngles = d.r;
        let trans = this.node.getComponent(UITransform);
        trans.setContentSize(d.s.x, d.s.y);
        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(BoxCollider2D);
        if (collider) {
            collider.size = trans.contentSize;
            collider.apply();
            let cw = collider.size.x * 0.5;
            this.maxX = cw + this.node.position.x;
            this.minX = -cw + this.node.position.x;

            //    collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            //    collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
        //
        let sp = this.node.getComponent(Sprite);
        if (sp) {
            sp.enabled = GlobalConfig.isShow2dCollider;
        }
    }

    onEnable() {
    }

    onDisable() {
    }


    //    // 只在两个碰撞体开始接触时被调用一次
    //    onBeginContact(selfCollider: BoxCollider2D, otherCollider: CircleCollider2D, contact: IPhysics2DContact | null) {
    //        // 被碰撞的物体-速度修正
    //        let groupId = 6; //
    //        if (otherCollider.body[GlobalEnum.ExtralProp.addLineVec] && otherCollider.group == 1 << groupId) {
    //            //上下方向
    //            if (otherCollider.node.position.x <= this.maxX + otherCollider.radius &&
    //                otherCollider.node.position.x >= this.minX - otherCollider.radius) {
    //                otherCollider.body[GlobalEnum.ExtralProp.addLineVec].x = otherCollider.body.linearVelocity.x > 0 ? this.addSpd : -this.addSpd;
    //            } else {
    //                otherCollider.body[GlobalEnum.ExtralProp.addLineVec].y = otherCollider.body.linearVelocity.y > 0 ? this.addSpd : -this.addSpd;
    //            }
    //        }
    //    }

    //    onEndContact(selfCollider: BoxCollider2D, otherCollider: CircleCollider2D, contact: IPhysics2DContact | null) {
    //        // 被碰撞的物体-速度修正
    //        let groupId = 6; //玩家
    //        if (otherCollider.body[GlobalEnum.ExtralProp.addLineVec] && otherCollider.group == 1 << groupId) {
    //            otherCollider.body[GlobalEnum.ExtralProp.addLineVec].set(0, 0);
    //        }
    //    }

}

